package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.exception.LogicalException;
import cn.wolfcode.wolf2w.query.TravelCommentQueryObject;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.util.PagingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
//@Transactional
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private TravelCommentRepository repository;

    @Autowired
    private MongoTemplate template;


    @Override
    public void save(TravelComment travelComment) {
        travelComment.setId(null);   //设为空值,防止数据库不会自动创建id

        //设值创建时间
        travelComment.setCreateTime(new Date());

        //判断是否有评论的评论
        if (StringUtils.hasLength(travelComment.getRefComment().getId())){
            //存在
            //获取评论的评论并设置
            travelComment.setRefComment(this.get(travelComment.getRefComment().getId()));
            //将评论的状态设为评论的评论
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE);
        }else {
            //没有,评论类别设为0
            //将评论的状态设为普通评论
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);
        }

        repository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);

    }

    @Override
    public void update(TravelComment travelComment) {
        repository.save(travelComment);
    }

    @Override
    public TravelComment get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<TravelComment> list() {
        return repository.findAll();
    }

    @Override
    public Page<TravelComment> query(TravelCommentQueryObject qo) {

        //查询封装类
        Query query = new Query();


        //查询分页
         Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.DESC, "createTime");

        //调用工具方法,返回分页数据
        return PagingUtil.query(query, template, TravelComment.class, pageable);

    }

    @Override
    public void saveOrUpdate(TravelComment travelComment) {
        //判断是否有id
        if (StringUtils.hasLength(travelComment.getId())){
            //这里是全量更新
            this.update(travelComment);
        }else {
            this.save(travelComment);
        }
    }


    @Override
    public List<TravelComment> queryByTravelId(String travelId) {



        return  repository.findByTravelIdOrderByCreateTime(travelId);
    }


}
